﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

namespace ClinicalMassageSystem
{
    class BackupRestore
    {
        //GetServerMethod
        public static Server GetSqlServer(string server, bool integratedSecurity, string loginName, string loginPassword)
        {
            try
            {
                // creates an instance of the SQL Server using server as the path
                Server serverInstance = new Server(server);
                if (integratedSecurity)
                {
                    serverInstance.ConnectionContext.LoginSecure = false;
                    serverInstance.ConnectionContext.Login = loginName;
                    serverInstance.ConnectionContext.Password = loginPassword;
                }
                return serverInstance;
            }
            catch (Exception ex)
            {
                throw new Exception("GetSqlServer threw " + ex.Message);
            }
        }

        // GetDatabaseMethod
        public static Database GetDatabase(Server server, string database)
        {
            // Checks for the database in the SQL Server
            if (server.Databases.Contains(database))
            {
                Database db = server.Databases[database];
                return db;
            }

            // throws an argument exception if the database is not found
            else
            {
                throw new ArgumentException("Database: " + database + " not found");
            }
        }

        // PerformBackup
        public static bool PerformBackup(Server server, Database db)
        {
            try
            {
                //Define a Backup object variable. 
                Backup backup = new Backup();

                DateTime time = DateTime.Now;     // Use current time
                string format = "MMM dd yyyy";    // Use this format
                string date = time.ToString(format);

                //Specify the type of backup, the description, the name, and the database to be backed up. 
                backup.Action = BackupActionType.Database;
                backup.BackupSetDescription = "Full Backup Performed: " + date;
                string setName = "Full-" + date;
                backup.BackupSetName = setName;
                backup.Database = db.Name;

                //Declare a BackupDeviceItem by supplying the backup device file name in the constructor, and the type of device is a file. 
                BackupDeviceItem bdi = default(BackupDeviceItem);
                bdi = new BackupDeviceItem(setName, DeviceType.File);

                //Add the device to the Backup object. 
                backup.Devices.Add(bdi);

                //Set the Incremental property to False to specify that this is a full database backup. 
                backup.Incremental = false;

                //Set the expiration date. 
                System.DateTime backupdate = new System.DateTime();
                time.AddDays(7);
                backupdate = new DateTime(time.Year, time.Month, time.Day);
                backup.ExpirationDate = backupdate;

                //Specify that the log must be truncated after the backup is complete. 
                backup.LogTruncation = BackupTruncateLogType.Truncate;

                //Run SqlBackup to perform the full database backup on the instance of SQL Server. 
                backup.SqlBackup(server);

                //Remove the backup device from the Backup object. 
                backup.Devices.Remove(bdi);

                return true;
            }
            catch
            {
                return false;
            }
        }

        public static BackupDeviceItem GetBackupDeviceItem(string deviceItem)
        {
            try
            {
                BackupDeviceItem bdi = default(BackupDeviceItem);
                bdi = new BackupDeviceItem(deviceItem, DeviceType.File);
                return bdi;
            }
            catch
            {
                throw new ArgumentException("File: " + deviceItem + " is not found");
            }
        }

        public static bool PerformRestore(Server server, BackupDeviceItem bdi, Database db, string databaseName)
        {
            try
            {
                //Define a Restore object variable.
                Restore rs = new Restore();
                db.Drop();

                //Set the NoRecovery property to false, so the transactions are recovered. 
                rs.NoRecovery = false;

                //Add the device that contains the full database backup to the Restore object. 
                rs.Devices.Add(bdi);

                //Specify the database name. 
                rs.Database = databaseName;

                //Restore the full database backup with no recovery. 
                rs.SqlRestore(server);

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
